package com.jochen.core.system

import com.artemis.Aspect
import com.artemis.annotations.Wire
import com.artemis.systems.IteratingSystem
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.badlogic.gdx.math.Polygon
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.math.Vector3
import com.jochen.core.components.ClickComponent
import com.jochen.core.components.Transform
import com.jochen.core.ui.realize.BuilingSelectUi
import com.jochen.core.audio.AudioManager
import com.jochen.core.world.GdxGame
import com.jochen.core.assets.*

import com.jochen.core.tools.*

class ClickSystem : IteratingSystem(Aspect.all(ClickComponent::class.java)), InputProcessor {

    @Wire private lateinit var buildingSelectUi: BuilingSelectUi
    @Wire private lateinit var camera: OrthographicCamera
    @Wire private lateinit var debugRenderer: ShapeRenderer
    @Wire private lateinit var game: GdxGame
    
    private var isDebug = true
    private var curBuildings = -1

    private val touchPos = Vector2()
    private val touchPosV3 = Vector3()
    private val pointer0Pos = Vector2()
    
    lateinit var audioManager: AudioManager
    
    override fun initialize(){
        audioManager = game.audioManager
    }
    
    override fun process(entityId: Int) {
        val transform = Mapper.transforms.get(entityId) ?: return
        val click = Mapper.clicks.get(entityId).also {c ->
            c.clickPolygon.updatePolygon(transform)
        }

        
        if (isDebug) {
            debugRenderer.color = if (click.isSelected) Color.RED else Color.GREEN
            debugRenderer.polygon(click.clickPolygon.transformedVertices)
        }
    }

    override fun begin() {
        if (isDebug) {
            debugRenderer.begin(ShapeRenderer.ShapeType.Line)
            debugRenderer.projectionMatrix = camera.combined
        }
    }

    override fun end() {
        if (isDebug) debugRenderer.end()
    }

    override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
        unproject(screenX.toFloat(), screenY.toFloat())
        
        if (pointer == 0) {
            pointer0Pos.set(touchPos)
            val entityIds = getEntityIds()
            for (i in 0 until entityIds.size()) {
                val entityId = entityIds.get(i)
                val click = Mapper.clicks.get(entityId)
                val building = Mapper.buildings.get(entityId)
                if (click.clickPolygon.contains(touchPos) && building != null) {
                    if (curBuildings != -1) clearBuilding()
                        click.isSelected = true
                        curBuildings = entityId
                        buildingSelectUi.setEntity(entityId)
                        audioManager.playSound(SoundAsset.BUILD)
                        return true
                    }
                    if (click.isSelected) clearBuilding()
                }
        } else if (curBuildings != -1) {
            clearBuilding()
        }
            return false
    }
    
    
    fun clearBuilding() {
        Mapper.clicks.get(curBuildings).isSelected = false
        curBuildings = -1
        buildingSelectUi.setEntity(curBuildings)
    }

    fun unproject(screenX: Float, screenY: Float) {
        touchPosV3.set(screenX,screenY,0f)
        camera.unproject(touchPosV3)
        touchPos.set(touchPosV3.x,touchPosV3.y)
    }

    companion object {
        fun updatePolygon(
            polygon: Polygon,position: Vector2,
            size: Vector2,scaling: Vector2,rotationDeg: Float) {
        val width = size.x * scaling.x
        val height = size.y * scaling.y

        val halfWidth = width * 0.5f
        val halfHeight = height * 0.5f
        val localVertices = floatArrayOf(
            -halfWidth, -halfHeight,  
            halfWidth, -halfHeight,   
            halfWidth, halfHeight,    
            -halfWidth, halfHeight  
        )
        polygon.vertices = localVertices 
        polygon.setOrigin(0f, 0f)
        polygon.rotation = rotationDeg 
        polygon.setPosition(
            position.x + halfWidth,  
            position.y + halfHeight
        )
    }
}
    override fun keyDown(keycode: Int) = false
    override fun keyUp(keycode: Int) = false
    override fun keyTyped(character: Char) = false
    override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int) = false
    override fun touchCancelled(screenX: Int, screenY: Int, pointer: Int, button: Int) = false
    override fun touchDragged(screenX: Int, screenY: Int, pointer: Int) = false
    override fun mouseMoved(screenX: Int, screenY: Int) = false
    override fun scrolled(amountX: Float, amountY: Float) = false
}